package cn.bocweb.spinning.view.forun;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.MyForumBean;
import cn.bocweb.spinning.util.DeviceUtil;

public class ForunMyTopicAdapter extends RecyclerView.Adapter {

    OnItemClickListener mOnItemClickListener;
    List<MyForumBean.DataBean> list;

    public ForunMyTopicAdapter(List<MyForumBean.DataBean> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_forun_my_topic, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        ViewHolder myHolder = (ViewHolder) holder;
        myHolder.mMyTopicTitle.setText(list.get(position).getTitle());
        myHolder.mMyTopicContent.setText(list.get(position).getContent());
        myHolder.mMyTopicTime.setText(
                DeviceUtil.getTimeByTwoTimeStampForPhp(
                        Long.parseLong(list.get(position).getTimeline()) * 1000,
                        System.currentTimeMillis()));
        int isVisible = list.get(position).getFlag().equals("0") ? View.GONE : View.VISIBLE;
        myHolder.mMyTopicTip.setVisibility(isVisible);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onClick(list.get(holder.getAdapterPosition()).getId());
            }
        });

        myHolder.mMyTopicZan.setText(list.get(position).getCount());

        int isHotVisible = list.get(position).getHot().equals("0") ? View.GONE : View.VISIBLE;
        myHolder.mMyTopicHots.setVisibility(isHotVisible);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public interface OnItemClickListener {
        void onClick(String id);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.my_topic_title)
        TextView mMyTopicTitle;
        @Bind(R.id.my_topic_content)
        TextView mMyTopicContent;
        @Bind(R.id.my_topic_tip_text)
        TextView mMyTopicTipText;
        @Bind(R.id.my_topic_tip)
        RelativeLayout mMyTopicTip;
        @Bind(R.id.my_topic_hots_text)
        TextView mMyTopicHotsText;
        @Bind(R.id.my_topic_hots)
        RelativeLayout mMyTopicHots;
        @Bind(R.id.my_topic_zan)
        TextView mMyTopicZan;
        @Bind(R.id.my_topic_time)
        TextView mMyTopicTime;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
